﻿using Arction.RenderingEngine;
using HighThroughput.Common;
using NPOI.DDF;
using Ozone.BLL;
using Ozone.Common;
using Ozone.Models;
using Ozone.Views.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using static HKSdkLibrary.CHCNetSDK.NET_DVR_DECCHANSTATUS.objectInfo;

namespace Ozone.Views.UserCenter
{
    /// <summary>
    /// WindowUserInfo.xaml 的交互逻辑
    /// </summary>
    public partial class WindowUserInfo : Window
    {
        public sys_user sys_UserModel;

        public WindowUserInfo(sys_user userModel)
        {
            InitializeComponent();
            cmbRole.ItemsSource = HighThroughput.Common.Tools.GetEnumList<InfoEnum.RoleType>();
            cmbRole.SelectedIndex = 0;
            if (userModel != null)
            {
                sys_UserModel = userModel;
                txtUserCode.Text = userModel.UserID;
                txtUserName.Text = userModel.Username;
                txtPassword.Text = userModel.Password;
                cmbRole.SelectedIndex = int.Parse(userModel.RoleId);
            }
        }

        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            // 验证输入
            if (string.IsNullOrWhiteSpace(txtUserCode.Text))
            {
                HighThroughput.Common.Tools.ShowPromptDialog(InfoEnum.DialogType.Warning.ToString(), "请输入用户编码！");

                txtUserCode.Focus();
                return;
            }

            if (string.IsNullOrWhiteSpace(txtUserName.Text))
            {
                HighThroughput.Common.Tools.ShowPromptDialog(InfoEnum.DialogType.Warning.ToString(), "请输入用户名！");
                txtUserName.Focus();
                return;
            }

            if (string.IsNullOrWhiteSpace(txtPassword.Text))
            {
                HighThroughput.Common.Tools.ShowPromptDialog(InfoEnum.DialogType.Warning.ToString(), "请输入密码！");
                txtPassword.Focus();
                return;
            }

            if (sys_UserModel == null) //新增
            {
                //需要判断用户编码是否存在，如果存在不能保存
                List<sys_user> sys_UsersList = Sys_UserBLL.GetUserInfoList(txtUserCode.Text, null);
                if (sys_UsersList == null || sys_UsersList.Count == 0)
                {
                    sys_UserModel = new Models.sys_user();
                    sys_UserModel.ID = Guid.NewGuid().ToString().ToUpper();
                    sys_UserModel.UserID = txtUserCode.Text.Trim();
                    sys_UserModel.Username = txtUserName.Text.Trim();
                    sys_UserModel.Password = txtPassword.Text;
                    sys_UserModel.RoleId = cmbRole.SelectedIndex.ToString();
                    sys_UserModel.Status = 1;
                    sys_UserModel.CreateTime = DateTime.Now;
                    sys_UserModel.LastLogin = DateTime.Now;
                }
                else
                {
                    HighThroughput.Common.Tools.ShowPromptDialog(InfoEnum.DialogType.Warning.ToString(), "此用户编码已存在！");
                    return;
                }
            }
            else    //修改
            {
                sys_UserModel.UserID = txtUserCode.Text;
                sys_UserModel.Username = txtUserName.Text;
                sys_UserModel.Password = txtPassword.Text;
                sys_UserModel.RoleId = cmbRole.SelectedIndex.ToString();
                sys_UserModel.LastLogin = DateTime.Now;
            }

            DialogResult = true;
            // 清空数据
            ClearForm();
        }

        private void BtnClear_Click(object sender, RoutedEventArgs e)
        {
            ClearForm();
        }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            //WindowConfirmationDialog confirmationDialog = new WindowConfirmationDialog("确认要关闭吗？");
            //if (confirmationDialog.ShowDialog() == true)
            //{
            //}
            DialogResult = false;
            this.Close();
        }

        private void ClearForm()
        {
            txtUserCode.Clear();
            txtUserName.Clear();
            txtPassword.Clear();
            cmbRole.SelectedIndex = 0;
        }
    }
}